<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
</head>
<body>

</body>
<script>
	Array.prototype.myReduce = function(callback,initialValue){
		if(this === undefined) throw new TypeError('this is null or not defined')
		if (Object.prototype.toString.call(callback) !== '[object Function]') throw new TypeError('callback is not function')
		let k = 0
		if(initialValue === undefined){
			if (this.length > 0){
				initialValue = this[0]
				k = 1
			} else {
				throw new Error('初始值不能为空')
			}
		}
		while (k < this.length){
			initialValue = callback(initialValue,this[k],k,this)
			k++
		}
		return initialValue
	}
	let arr = [1,2,3,4,5,6,7,8]
	console.log(arr.reduce((acc,num) => {
		return acc + num
	}))
	console.log(arr.myReduce((acc,num) => {
		return acc + num
	}))
</script>
</html>
